<?php

namespace app\index\controller;

use think\Db as resumeDB;
use think\Log;

class ScholarResume extends Common{

    public $datas;

    /**
     * 新增过往履历
     */
    public function addScholarResume(){
        $this->datas = $this->params;


        try {
            $insertData = [];
            $insertData['kid'] = $this->datas['kid'];
            $insertData['year_from'] = $this->datas['yearFrom'];
            $insertData['year_to'] = $this->datas['yearTo'];
            $insertData['agency'] = $this->datas['agency'];
            $insertData['education'] = $this->datas['education'];
            $insertData['email'] = $this->datas['email'];

            resumeDB::table('scholar_resume')->insert($insertData);


            $insertData['operate'] = 'insert';
            resumeDB::table('scholar_resume_history')->insert($insertData);

            $this->returnMsg(1, '操作成功！');
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 修改过往履历
     */
    public function updateScholarResume(){
        $this->datas = $this->params;

        try {

            resumeDB::name('scholar_resume')->where([
                'id'=>$this->datas['id']
            ])->update([
                'year_from'=>$this->datas['yearFrom'],
                'year_to'=>$this->datas['yearTo'],
                'agency'=>$this->datas['agency'],
                'education'=>$this->datas['education'],
                'email'=>$this->datas['email'],
                'update_time'=>date('Y-m-d H:i:s')
            ]);

            $this->addDelScholarResumeHistory($this->datas['id'],'update');

            $this->returnMsg(1, '操作成功！');
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 根据kid查询所有履历
     */
    public function getScholarResume(){
        $this->datas = $this->params;


        try {
            $sql = "SELECT id,kid,year_from yearFrom,year_to yearTo,agency,education,email FROM scholar_resume "
                    ."WHERE kid = '".$this->datas['kid']."' "
                    ."ORDER BY year_from";
            $result = resumeDB::query($sql);

            if(empty($result)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $result);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }

    /**
     * 删除履历
     */
    public function delScholarResume(){
        $this->datas = $this->params;


        try {
            $this->addDelScholarResumeHistory($this->datas['id'],'delete');


            $count = resumeDB::name('scholar_resume')->where([
                'id'=>$this->datas['id']
            ])->delete();


            if(empty($count)){
                $this->returnMsg(9, '删除失败！');
            }

            $this->returnMsg(1, '删除数据成功！');
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }

    /**
     * 添加历史记录
     */
    private function addDelScholarResumeHistory($id,$operate){
        try {

            $sql = "SELECT kid,year_from yearFrom,year_to yearTo,agency,education,email FROM scholar_resume WHERE id = ".$id;
            $result = resumeDB::query($sql);

            if(!empty($result)){
                $insertData = [];
                $insertData['kid'] = $result[0]['kid'];
                $insertData['year_from'] = $result[0]['yearFrom'];
                $insertData['year_to'] = $result[0]['yearTo'];
                $insertData['agency'] = $result[0]['agency'];
                $insertData['education'] = $result[0]['education'];
                $insertData['email'] = $result[0]['email'];
                $insertData['operate'] = $operate;

                resumeDB::table('scholar_resume_history')->insert($insertData);
            }
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }
}